﻿using System;
using System.Text;
using System.Net;
using System.IO;
using Utilities.Log;
namespace Wx.Core
{
    public static class HttpUtility
    {
        private static LogHelper log = new LogHelper(typeof(HttpUtility));
        private static HttpWebRequest request;
        private static HttpWebResponse response;
        public static Model.HttpResult Get(string url, string contentType = "application/x-www-form-urlencoded")
        {
            try
            {
                request = WebRequest.Create(url) as HttpWebRequest;
                request.KeepAlive = true;
                request.Method = "GET";
                request.ContentType = contentType;
                using (response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    return new Model.HttpResult()
                    {
                        status = true,
                        msg = reader.ReadToEnd()
                    };
                }
            }
            catch (Exception e)
            {
                log.error(e);
                return new Model.HttpResult()
                {
                    status = false,
                    msg = e.Message
                };
            }
        }

        public static Model.HttpResult Post(string url, string requestData, string contentType = "application/x-www-form-urlencoded")
        {
            try
            {
                request = WebRequest.Create(url) as HttpWebRequest;
                request.KeepAlive = true;
                request.Method = "POST";
                byte[] postBytes = null;
                request.ContentType = contentType;
                postBytes = Encoding.UTF8.GetBytes(requestData);
                request.ContentLength = postBytes.Length;
                using (Stream outstream = request.GetRequestStream())
                {
                    outstream.Write(postBytes, 0, postBytes.Length);
                }
                string result = string.Empty;
                using (WebResponse response = request.GetResponse())
                {
                    if (response != null)
                    {
                        using (Stream stream = response.GetResponseStream())
                        {
                            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                            {
                                return new Model.HttpResult()
                                {
                                    status = true,
                                    msg = reader.ReadToEnd()
                                };
                            }
                        }

                    }
                    else
                    {
                        return new Model.HttpResult()
                        {
                            status = false,
                            msg = "response is null!"
                        };
                    }
                }
            }
            catch (Exception e)
            {
                log.error(e);
                return new Model.HttpResult()
                {
                    status = false,
                    msg = e.Message
                };
            }
        }

        public static void DownloadFile(string url, string fileName)
        {
            WebClient web = new WebClient();
            web.DownloadFile(url, fileName);
        }
    }
}
